package com.kinyx.framework.db.sentence.str;

import java.util.ArrayList;
import java.util.List;

import com.kinyx.framework.db.sentence.defines.Sentence;
import com.kinyx.framework.exception.KRuntimeException;

/**
 * SQL语句对象
 */
public class SQLBatch implements Sentence {

	/** 语句 */
	private StringBuilder sql = new StringBuilder();
	/** 参数 */
	private List<List<Object>> params = new ArrayList<>();

	/**
	 * 追加语句
	 * @param sql 语句
	 * @return 本对象
	 */
	public SQLBatch s(final String sql) {
		this.sql.append(sql);
		return this;
	}

	/**
	 * 追加参数
	 * @param param 参数
	 * @param index 索引值
	 * @return 本对象
	 */
	public SQLBatch p(final Object param, final int index) {
		if (index < this.params.size()) {
			final List<Object> list = this.params.get(index);
			list.add(param);
		} else if (index == this.params.size()) {
			final List<Object> list = new ArrayList<>();
			list.add(param);
			this.params.add(list);
		} else {
			throw new KRuntimeException(new ArrayIndexOutOfBoundsException(index));
		}
		return this;
	}

	/** @return 取语句 */
	@Override
	public String getSentence() { return this.sql.toString(); }

	/** @return 取参数 */
	public List<List<Object>> getListParams() { return this.params; }

}
